<script setup>
  import { ref } from 'vue'
  import { loginApi } from '@/api/login';
  import { ElMessage } from 'element-plus';
  import { useRouter } from 'vue-router';

  let loginForm = ref({username:'', password:''})
  let router=useRouter();
  // 登录
  const login= async ()=>{
    let result=await loginApi(loginForm.value);
    if(result.code){
      ElMessage.success("登录成功");
      // 保存token及员工信息
      localStorage.setItem("loginUser",JSON.stringify(result.data));// 保存到本地,JSON.stringify的作用是把JSON对象转换成字符串
      // 删除学生的token
      localStorage.removeItem('Student');
      localStorage.removeItem('no');
      // 跳转页面 - 跳转到首页
      router.push('/index');
    }else{
      ElMessage.error(result.msg);
    }
  };

  // 重置
  const clear=()=>{
    loginForm.value={username:'', password:''};
  };
  
</script>

<template>
  <div id="container">
    <div class="login-form">
      <el-form label-width="80px">
        <p class="title">管理辅助系统</p>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="button" type="primary" @click="login">登 录</el-button>
          <el-button class="button" type="info" @click="clear">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
#container {
  padding: 10%;
  height: 410px;
  background-image: url('../../assets/bg1.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}

.login-form {
  max-width: 400px;
  padding: 30px;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  background-color: white;
}

.title {
  font-size: 30px;
  font-family: '楷体';
  text-align: center;
  margin-bottom: 30px;
  font-weight: bold;
}

.button {
  margin-top: 30px;
  width: 120px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  #container {
    height: auto;
    min-height: 100vh;
    padding: 20px;
    background-size: cover;
    background-position: center;
    display: flex;
    align-items: center;
  }

  .login-form {
    width: 90%;
    padding: 25px;
    margin: 0 auto;
    background-color: rgba(255, 255, 255, 0.95);
  }

  .title {
    font-size: 24px;
    margin-bottom: 25px;
  }

  /* 调整表单元素间距 */
  .el-form-item {
    margin-bottom: 20px;
  }

  /* 调整输入框高度 */
  .el-input :deep(.el-input__inner) {
    height: 40px;
  }

  /* 按钮适配 */
  .el-form-item__content {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
  }

  .button {
    width: 100%;
    margin-top: 15px;
    height: 42px;
  }
}

/* 小屏幕手机优化 */
@media (max-width: 480px) {
  .login-form {
    padding: 20px;
    border-radius: 8px;
  }

  .title {
    font-size: 22px;
    margin-bottom: 20px;
  }

  .el-form-item {
    margin-bottom: 15px;
  }

  /* 调整标签位置 */
  .el-form-item__label {
    padding-bottom: 6px !important;
  }

  /* 输入框字体优化 */
  .el-input :deep(.el-input__inner) {
    font-size: 14px;
    padding: 0 12px;
  }

  .button {
    font-size: 14px;
  }
}
</style>